#include <iostream>
using namespace std;
const int NameLen = 20;

void order(char name[][NameLen], int n) // 字符串排序
{
    char temp[NameLen];
    for (int i = 0; i < n - 1; i++) // 选择排序
    {
        for (int j = i + 1; j < n; j++) // 比较两个字符串的大小
        {
            if (strcmp(name[i], name[j]) > 0) // 比较两个字符串的大小
            {
                // 字符串交换
                strcpy(temp, name[i]);
                strcpy(name[i], name[j]);
                strcpy(name[j], temp);
            }
        }
    }
}

int find(char name[][NameLen], int n, char searchname[NameLen])
{
    for (int i = 0; i < n; i++)
    {
        if (strcmp(name[i], searchname) == 0) // 找到, 返回位置
        {
            return i + 1;
        }
        if (strcmp(name[i], searchname) > 0) // 未找完, 但找不到, 返回0
        {
            return 0;
        }
    }
    return 0; // 找完, 找不到, 返回0
}

inline void intro()
{
    cout << endl
         << "学号：3170608043，姓名：石英钰" << endl
         << endl
         << "p101例3-5 字符串排序与查找" << endl
         << endl;
}

int main()
{
    intro();
    char NameTab[][NameLen] = {
        "GongJing",
        "LiuNa",
        "HuangPin",
        "ZhouZijun",
        "LiaoXiaolei",
        "ChenHailing",
        "CuiPeng",
        "LiuPing",
    };
    char searchname[NameLen];
    int n = sizeof(NameTab) / NameLen;
    order(NameTab, n);
    for (int i = 0; i < n; i++) // 输出排序后的各姓名
    {
        cout << i + 1 << '\t' << NameTab[i] << endl;
    }
    cout << "Input the searching name:";
    cin >> searchname;

    n = find(NameTab, n, searchname);
    if (n)
    {
        cout << "Position:" << n << endl;
    }
    else
    {
        cout << "Not found!" << endl;
    }
    return 0;
}